package com.dcxs.shop.common.task;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dcxs.shop.module.order.entity.JsProductOrder;
import com.dcxs.shop.module.order.service.IJsProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling
public class Task {
    @Autowired
    private IJsProductOrderService productOrderService;


    @Scheduled(fixedRate=300000)
    private void configureTasks() {
        //查询所有待支付的订单
        List<JsProductOrder> list = productOrderService.list(new QueryWrapper<JsProductOrder>().eq("state",0));
        List<String> list1 = new ArrayList<>();
        for(JsProductOrder productOrder:list){
            if(!DateUtil.isIn(new Date(),Date.from(productOrder.getInsertDatetime().atZone( ZoneId.systemDefault()).toInstant()),DateUtil.offsetDay(Date.from(productOrder.getInsertDatetime().atZone( ZoneId.systemDefault()).toInstant()),1))){
                list1.add(productOrder.getOrderNumber());
            }
        }
        if(CollectionUtil.isNotEmpty(list1)){
            UpdateWrapper<JsProductOrder> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("order_number",list1).set("state",99);
            productOrderService.update(updateWrapper);
        }
    }
}
